package com.bcfrj.shop.controller;

import com.bcfrj.shop.pojo.User;
import com.bcfrj.shop.service.impl.UserServiceImpl;
import com.bcfrj.shop.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    @PostMapping( "/login")
    public Result login(String account,String password){
        System.out.println(account+","+password);
        User user = userService.queryUserByAccount(account);
        System.out.println(user);
        if (user!=null){
            if (password.equals(user.getPassword())){
                return Result.ok("dw4a65sd56w4a56s4d65wasd665w4da6");
            }
        }
        return Result.fail("账号或密码错误!");
    }

    @PostMapping("/register")
    public Result register(User user){
        System.out.println(user);
        if (user!=null && user.getAccount()!=null && user.getPassword()!=null && user.getNickName()!=null);
        Integer rows = userService.insertUser(user);
        if (rows!=0){
            return Result.ok("注册成功");
        }
        else{
            return Result.fail("账号已被注册！");
        }
    }

}
